<?php
class CMRPageShowResource extends CMRPage
{
	public function __construct($database, $ResourceID)
	{
		parent::__construct($database);
		$this->resourceID = $ResourceID;
	}
	
/*	private function logView()
	{
		$now = new DateTime("now");
		$sql = "INSERT INTO ViewedResources (ViewedWhen,ResourceID,UserID) VALUES ('".$now->format('Y-m-d H:i:s')."','".$this->resourceID."','".$this->UserID."')";
		$this->Database->ExecuteSQL($sql);
	}*/
	
	protected function getHTTPHeadersHTML()
	{
		$this->logView();
		$sql = "SELECT * from Resources WHERE ResourceID=".$_POST['ResourceID'];
		$this->Database->ExecuteSQL($sql);
		$row = $this->Database->getRow();
		$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
		$this->Title = $resource->getResourceTitle();
//		$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
		
//		echo $row['ResourceTypeID'];
		// if a link, redirect to site
		if ($row['ResourceTypeID']==7)
		{
			$sql = "SELECT * from Links WHERE ResourceIDP=".$_POST['ResourceID'];
			$this->Database->ExecuteSQL($sql);
			$row = $this->Database->getRow();
			
			header("Location: ".$row['URL']);
			exit();
		}
		
/*		if ($row['ResourceTypeID']==6)
		{
//			print_r($_POST);
		
			$fileID = $_POST['FileID'];
			$tableName = $_POST['DBTableName'];
	
			$sql = "SELECT FSFileName,FSFileSize,FSFileType FROM ".$tableName." WHERE PhysicalFileNumber = ".$fileID;
//			$sql = "SELECT FileID, FSFileName,FSFileSize,FSFileType FROM Files WHERE ResourceIDP = ".$this->resourceID;
//			echo $sql."<br>";
			$this->Database->ExecuteSQL($sql);
				
			list($fileID, $name, $type, $size) =  $this->Database->getRow(); 
//			echo $name." ".$type." ".$size." ".$recordID."<br>";
			$filename = "ResourceFiles/Files_FSFile_".$fileID;
			$content = file_get_contents($filename);
	
//			header("Content-length: $size");
			header("Content-type: $type");
			header("Content-Disposition: attachment; filename=\"$name\"");
			echo $content;
				
			$this->Database->close();
			exit;
		}*/
	}

	protected function getContentsDiv()
	{
		
		$script = new EditDeleteFormScript();
		$html = $script->getHTML();
		
		
		
		$sql = "SELECT * from Resources WHERE ResourceID=".$_POST['ResourceID'];
		$this->Database->ExecuteSQL($sql);
		$row = $this->Database->getRow();
		
		// is this necessary ....
//		$resource = new Resource($this->Database->Recopy(),$row['ResourceID']);
//		$resource->loadData($row['ResourceID'],$row['ResourceTitle'],$row['ResourceDescription'],$row['ResourceTypeID'],"","",,,,);
		
		$html .= "<div id=\"content\">\n";
		$html .= "<h1>".$row['ResourceTitle']."</h1>\n";
		
/*		$sectionsPanel = new SectionsPanel('');
		$html .= $sectionsPanel->getHTML();
		
		$shortcutPanel = new ShortcutPanel('');
		$html .= $shortcutPanel->getHTML();
		
		
		$html .= "<hr>\n";		
*/		
		if(($this->userGroup=="Editors")||($this->userGroup=="Supervisors"))
		{
			$hidden = new Hidden();
			$hidden->setName("SubAction");
			$html .= $hidden->getHTML();
		
			$hidden = new Hidden();
			$hidden->setName("ResourceID");
			$html .= $hidden->getHTML();
		
			$html .= "<span class=\"SummaryAdminControls\" id=\"SummaryAdminEdit".$this->resourceID."\" onMouseOver=\"OnMouseOverSummaryAdminEdit('SummaryAdminEdit".$this->resourceID."');\" onMouseOut=\"OnMouseOutSummaryAdminEdit('SummaryAdminEdit".$this->resourceID."');\" onClick=\"OnClickEditResource('".$this->resourceID."');\">Edit</span>\n";
			
			
		}
		
		
		switch ($row['ResourceTypeID'])
		{
			case 2: // Repertoire item
				$resource = new ResourceRepertoireInfo($this->Database->Recopy(),$row['ResourceID']);
				$html .= $resource->getDataHTML();
				$html .= $resource->getFullHTML();
				break;
				
			case 3: // Composer
				$resource = new ResourceComposer($this->Database->Recopy(),$row['ResourceID']);
				$html .= $resource->getDataHTML();
				$html .= $resource->getFullHTML();
				break;
				
			case 6: // File
				$resource = new ResourceFile($this->Database->Recopy(),$row['ResourceID']);
				$html .= $resource->getFullHTML();
				$html .= $resource->getDataHTML();
				break;
				
			case 7: // link
			default:
				break;
		}
				
		$html .= "</div>\n";
		return $html;
	}
	
}